package com.myBlog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.myBlog.constants.SystemConstants;
import com.myBlog.domain.ResponseResult;
import com.myBlog.domain.dto.LinkDTO;
import com.myBlog.domain.entity.Link;
import com.myBlog.domain.vo.LinkVo;
import com.myBlog.domain.vo.PageVo;
import com.myBlog.enums.AppHttpCodeEnum;
import com.myBlog.exception.SystemException;
import com.myBlog.mapper.LinkMapper;
import com.myBlog.service.LinkService;
import com.myBlog.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 友链(Link)表服务实现类
 *
 * @author makejava
 * @since 2023-03-14 17:11:00
 */
@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {

    @Override
    public ResponseResult getAllLink() {
        LambdaQueryWrapper<Link> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Link::getStatus, SystemConstants.LINK_STATUS_NORMAL);
        List<Link> links = this.list(wrapper);
        //转换成Vo
        List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(links, LinkVo.class);
        //封装返回
        return ResponseResult.okResult(linkVos);
    }

    @Override
    public ResponseResult<PageVo> list(Integer pageNum, Integer pageSize, LinkDTO linkDTO) {
        LambdaQueryWrapper<Link> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(linkDTO.getName()), Link::getName, linkDTO.getName());
        wrapper.eq(StringUtils.hasText(linkDTO.getStatus()), Link::getStatus, linkDTO.getStatus());
        Page<Link> linkPage = new Page<>();
        page(linkPage, wrapper);
        PageVo pageVo = new PageVo(linkPage.getRecords(), linkPage.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult getLinkInfo(Long linkId) {
        Link link = getById(linkId);
        LinkDTO linkDTO = BeanCopyUtils.copyBean(link, LinkDTO.class);
        return ResponseResult.okResult(linkDTO);
    }

    @Override
    public ResponseResult updateLink(LinkDTO linkDTO) {
        if (null == linkDTO.getId() || !StringUtils.hasText(linkDTO.getName())) {
            throw new SystemException(AppHttpCodeEnum.LINKNAME_NOT_NULL);
        } else {
            Link link = BeanCopyUtils.copyBean(linkDTO, Link.class);
            updateById(link);
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delete(List<Long> ids) {
        if (ids.size() > 0) {
            List<Link> links = listByIds(ids);
            links = links.stream()
                    .peek(link -> link.setDelFlag(1))
                    .collect(Collectors.toList());
            removeBatchByIds(links);
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult addLink(LinkDTO linkDTO) {
        if (!StringUtils.hasText(linkDTO.getName())) {
            throw new SystemException(AppHttpCodeEnum.TAGNAME_NOT_NULL);
        }
        Link link = BeanCopyUtils.copyBean(linkDTO, Link.class);
        save(link);
        return ResponseResult.okResult();
    }
}

